home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / MarkerPick / MarkerPickSupport.c < prev    next >
Encoding:
Text File  |  1997-08-14  |  8.5 KB  |  326 lines  |  [TEXT/MPCC]

  1. // My3dSupport.c - QuickDraw 3d routines
  2. //
  3. // This file contains utility routines for QuickDraw 3d sample code.
  4. // This is a simple QuickDraw 3d application to draw a cube in the center 
  5. // of the main application window.  The routines in here handle setting up
  6. // the main display group, the view, the Macintosh 3D draw context, and the
  7. // camera and lighting. 
  8. //
  9. // This code is the basis of the introductory article in  d e v e l o p  issue 22
  10. //
  11. // Nick Thompson - January 6th 1994
  12. // 
  13. // ©1994-95 Apple computer Inc., All Rights Reserved
  14. //
  15.  
  16. #include <QuickDraw.h>
  17. #include <QDOffScreen.h>
  18.  
  19.  
  20. #include "MarkerPickSupport.h"
  21.  
  22.  
  23. #include "QD3DDrawContext.h"
  24. #include "QD3DRenderer.h"
  25. #include "QD3DShader.h"
  26. #include "QD3DCamera.h"
  27. #include "QD3DLight.h"
  28. #include "QD3DGeometry.h"
  29. #include "QD3DGroup.h"
  30. #include "QD3DMath.h"
  31. #include "QD3DSet.h"
  32. #include "QD3DTransform.h"
  33. #include "QD3DAcceleration.h"
  34.  
  35. static     TQ3Point3D    documentGroupCenter;
  36. static    float        documentGroupScale;
  37.  
  38.  
  39. TQ3ViewObject MyNewView(WindowPtr theWindow)
  40. {
  41.     TQ3Status                myStatus;
  42.     TQ3ViewObject            myView;
  43.     TQ3DrawContextObject        myDrawContext;
  44.     TQ3RendererObject        myRenderer;
  45.     TQ3CameraObject            myCamera;
  46.     TQ3GroupObject            myLights;
  47.     
  48.     myView = Q3View_New();
  49.     
  50.     //    Create and set draw context.
  51.     if ((myDrawContext = MyNewDrawContext(theWindow)) == nil )
  52.         goto bail;
  53.         
  54.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  55.         goto bail;
  56.  
  57.     Q3Object_Dispose( myDrawContext ) ;
  58.     
  59.     //    Create and set renderer.
  60.     
  61.     
  62.     
  63.     // this would use the Z-Buffer renderer
  64. #if 0
  65.  
  66.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
  67.     if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  68.         goto bail;
  69.     }
  70.     
  71. #else
  72.  
  73.     // this would use the interactive software renderer
  74.  
  75.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
  76.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure ) {
  77.             goto bail;
  78.         }
  79.         // these two lines set us up to use the best possible renderer,
  80.         // including  hardware if it is installed.
  81.         Q3InteractiveRenderer_SetDoubleBufferBypass (myRenderer, kQ3True);                        
  82.         Q3InteractiveRenderer_SetPreferences(myRenderer, kQAVendor_BestChoice, 0);
  83.  
  84.     }
  85.     else {
  86.         goto bail;
  87.     }
  88. #endif
  89.  
  90.     Q3Object_Dispose( myRenderer ) ;
  91.     
  92.     //    Create and set camera.
  93.     if ( (myCamera = MyNewCamera(theWindow)) == nil )
  94.         goto bail;
  95.         
  96.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  97.         goto bail;
  98.  
  99.     Q3Object_Dispose( myCamera ) ;
  100.     
  101.     //    Create and set lights.
  102.     if ((myLights = MyNewLights()) == nil )
  103.         goto bail;
  104.         
  105.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  106.         goto bail;
  107.         
  108.     Q3Object_Dispose(myLights);
  109.  
  110.     //    Done!!!
  111.     return ( myView );
  112.     
  113. bail:
  114.     //    If any of the above failed, then don't return a view.
  115.     return ( nil );
  116. }
  117.  
  118. //----------------------------------------------------------------------------------
  119.  
  120. TQ3DrawContextObject MyNewDrawContext(WindowPtr theWindow)
  121. {
  122.     TQ3DrawContextData        myDrawContextData;
  123.     TQ3MacDrawContextData    myMacDrawContextData;
  124.     TQ3ColorARGB            ClearColor;
  125.     TQ3DrawContextObject    myDrawContext ;
  126.     
  127.     //    Set the background color.
  128.     ClearColor.a = 1.0;
  129.     ClearColor.r = 1.0;
  130.     ClearColor.g = 1.0;
  131.     ClearColor.b = 1.0;
  132.     
  133.     //    Fill in draw context data.
  134.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  135.     myDrawContextData.clearImageColor = ClearColor;
  136.     myDrawContextData.paneState = kQ3False;
  137.     myDrawContextData.maskState = kQ3False;
  138.     myDrawContextData.doubleBufferState = kQ3True;
  139.  
  140.     myMacDrawContextData.drawContextData = myDrawContextData;
  141.     
  142.     myMacDrawContextData.window = (CGrafPtr) theWindow;        // this is the window associated with the view
  143.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  144.     myMacDrawContextData.viewPort = nil;
  145.     myMacDrawContextData.grafPort = nil;
  146.     
  147.     //    Create draw context and return it, if it’s nil the caller must handle
  148.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData) ;
  149.  
  150.     return myDrawContext ;
  151. }
  152.  
  153. //----------------------------------------------------------------------------------
  154.  
  155. TQ3CameraObject MyNewCamera(WindowPtr theWindow)
  156. {
  157.     TQ3ViewAngleAspectCameraData    perspectiveData;
  158.     TQ3CameraObject                camera;
  159.     
  160.     TQ3Point3D                     from     = { 0.0, 0.0, 7.0 };
  161.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  162.     TQ3Vector3D                 up         = { 0.0, 1.0, 0.0 };
  163.  
  164.     float                         fieldOfView = 1.0;
  165.     float                         hither         = 0.001;
  166.     float                         yon         = 1000;
  167.     
  168.     TQ3Status                    returnVal = kQ3Failure ;
  169.  
  170.  
  171.     perspectiveData.cameraData.placement.cameraLocation     = from;
  172.     perspectiveData.cameraData.placement.pointOfInterest     = to;
  173.     perspectiveData.cameraData.placement.upVector             = up;
  174.  
  175.     perspectiveData.cameraData.range.hither    = hither;
  176.     perspectiveData.cameraData.range.yon     = yon;
  177.  
  178.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  179.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  180.     perspectiveData.cameraData.viewPort.width = 2.0;
  181.     perspectiveData.cameraData.viewPort.height = 2.0;
  182.     
  183.     perspectiveData.fov                = fieldOfView;
  184.     perspectiveData.aspectRatioXToY    =
  185.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  186.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  187.         
  188.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  189.  
  190.     return camera ;
  191. }
  192.  
  193.  
  194. //----------------------------------------------------------------------------------
  195.  
  196. TQ3GroupObject MyNewLights()
  197. {
  198.     TQ3GroupPosition        myGroupPosition;
  199.     TQ3GroupObject            myLightList;
  200.     TQ3LightData            myLightData;
  201.     TQ3PointLightData        myPointLightData;
  202.     TQ3DirectionalLightData    myDirectionalLightData;
  203.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  204.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  205.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  206.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  207.     
  208.     //    Set up light data for ambient light.  This light data will be used for point and fill
  209.     //    light also.
  210.  
  211.     myLightData.isOn = kQ3True;
  212.     myLightData.color = WhiteLight;
  213.     
  214.     //    Create ambient light.
  215.     myLightData.brightness = .2;
  216.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  217.     if ( myAmbientLight == nil )
  218.         goto bail;
  219.     
  220.     //    Create point light.
  221.     myLightData.brightness = 1.0;
  222.     myPointLightData.lightData = myLightData;
  223.     myPointLightData.castsShadows = kQ3False;
  224.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  225.     myPointLightData.location = pointLocation;
  226.     myPointLight = Q3PointLight_New(&myPointLightData);
  227.     if ( myPointLight == nil )
  228.         goto bail;
  229.  
  230.     //    Create fill light.
  231.     myLightData.brightness = .2;
  232.     myDirectionalLightData.lightData = myLightData;
  233.     myDirectionalLightData.castsShadows = kQ3False;
  234.     myDirectionalLightData.direction = fillDirection;
  235.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  236.     if ( myFillLight == nil )
  237.         goto bail;
  238.  
  239.     //    Create light group and add each of the lights into the group.
  240.     myLightList = Q3LightGroup_New();
  241.     if ( myLightList == nil )
  242.         goto bail;
  243.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  244.     if ( myGroupPosition == 0 )
  245.         goto bail;
  246.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  247.     if ( myGroupPosition == 0 )
  248.         goto bail;
  249.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  250.     if ( myGroupPosition == 0 )
  251.         goto bail;
  252.  
  253.     Q3Object_Dispose( myAmbientLight ) ;
  254.     Q3Object_Dispose( myPointLight ) ;
  255.     Q3Object_Dispose( myFillLight ) ;
  256.  
  257.     //    Done!
  258.     return ( myLightList );
  259.     
  260. bail:
  261.     //    If any of the above failed, then return nothing!
  262.     return ( nil );
  263. }
  264.  
  265.  
  266. TQ3GroupObject MyNewModel()
  267. {
  268.     TQ3GroupObject    myGroup = NULL;
  269.     TQ3Object        myMarker = NULL;
  270.     TQ3MarkerData    markerData;
  271.     unsigned char    memory[1000];
  272.     unsigned char*    ptr;
  273.     long            i;
  274.             
  275.     // Create a group for the complete model.
  276.     if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
  277.         
  278.         // set up the marker
  279.         Q3Point3D_Set(&markerData.location, 0.0, 0.0, 0.0);
  280.         markerData.xOffset            = -2;
  281.         markerData.yOffset            = -15;
  282.         markerData.bitmap.image        = memory;
  283.         markerData.bitmap.width        = 30;
  284.         markerData.bitmap.height    = 30;
  285.         markerData.bitmap.rowBytes    = Q3Bitmap_GetImageSize(30, 30) / 30;
  286.         markerData.bitmap.bitOrder    = kQ3EndianBig;
  287.         markerData.markerAttributeSet = NULL;
  288.         
  289.         // brute force method to set up the bitmap
  290.         ptr = markerData.bitmap.image;
  291.         for (i=0; i<8; i++)
  292.             {
  293.             *ptr++ = 0xff;
  294.             *ptr++ = 0xff;
  295.             *ptr++ = 0xff;
  296.             *ptr++ = 0xff;
  297.             }
  298.         for (i=0; i<14; i++)
  299.             {
  300.             *ptr++ = 0xfc;
  301.             *ptr++ = 0x00;
  302.             *ptr++ = 0x00;
  303.             *ptr++ = 0xff;
  304.             }
  305.         for (i=0; i<8; i++)
  306.             {
  307.             *ptr++ = 0xff;
  308.             *ptr++ = 0xff;
  309.             *ptr++ = 0xff;
  310.             *ptr++ = 0xff;
  311.             }
  312.             
  313.         // now create the marker
  314.         myMarker = Q3Marker_New(&markerData);
  315.         Q3Group_AddObject(myGroup, myMarker);    
  316.         
  317.         // dispose of the objects we created here
  318.         if( myMarker ) 
  319.             Q3Object_Dispose(myMarker);
  320.     }
  321.     
  322.     //    Done!
  323.     return ( myGroup );
  324. }
  325.  
  326.